﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YGWebMobileApi.Data;
using OneMile.Common;
using OneMile.Entity.Model;
using OneMile.BLL;

namespace YGWebMobileApi.APIs
{
    [MobileAPI(Code = "MC0300", Name = "用户提交新订单")]
    public class UAddNewOrder : IProcessMessage
    {
        private BLLUOrder bll;
        public BLLUOrder BLL
        {
            get
            {
                if (bll == null)
                    bll = BLLFactory.getInstance().CreateUOrderBLL();
                return bll;
            }
        }


        #region IProcessMessage 成员

        public MobileRspMsg ProcessMessage(MobileReqMsg reqMsg)
        {
            try
            {
        
                MobileRspMsg rspMsg = new MobileRspMsg(reqMsg);
                // 获取参数字段（具体请参见文档）
                EOrder order = new EOrder();
                order.OrderId = Guid.NewGuid();
                order.OrderMobile = reqMsg.Body.GetFieldValue("OrderMobile");
                order.OrderName = reqMsg.Body.GetFieldValue("OrderName");
                order.OrderRemarks = reqMsg.Body.GetFieldValue("OrderRemarks");
                order.OrderType = 1;//初始为未确认状态
                string prom = reqMsg.Body.GetFieldValue("PromotionsId");
                if (!String.IsNullOrEmpty(prom))
                {
                    order.PromotionsId = new Guid(prom);
                }
                string coupon = reqMsg.Body.GetFieldValue("CouponId");
                if (!String.IsNullOrEmpty(coupon))
                {
                    order.CouponId = coupon.TGuid();
                }
                order.ShopId = new Guid(reqMsg.Body.GetFieldValue("ShopId"));
                order.TotalPrice = reqMsg.Body.GetFieldValue("TotalPrice").TDecimal();
                order.UserId = new Guid(reqMsg.Body.GetFieldValue("UserId"));
                order.OrderAddress = reqMsg.Body.GetFieldValue("OrderAddress");
                order.LatestTime =String.Format("{0} {1}:00",DateTime.Now.ToShortDateString(),reqMsg.Body.GetFieldValue("LatestTime")).TDateTime(); 
                order.EarliestTime =String.Format("{0} {1}:00",DateTime.Now.ToShortDateString(),reqMsg.Body.GetFieldValue("EarliestTime")).TDateTime();
                order.Freight = reqMsg.Body.GetFieldValue("Freight").TDecimal();
                order.Rec_CreateBy = reqMsg.Body.GetFieldValue("Rec_CreateBy");
                order.Rec_ModifyBy = reqMsg.Body.GetFieldValue("Rec_ModifyBy");
                order.Disabled = 0;
                
                IList<EOrderDetails> listOD=new List<EOrderDetails>();
                EOrderDetails od=null;;
                var fl=reqMsg.Body.GetFieldList("OrderDetailsList");
                foreach(var flc in fl.FieldLists)
                {
                    od=new EOrderDetails();
                    od.OrderDetailsId=Guid.NewGuid();
                    od.OrderId=order.OrderId;
                    od.CommodityId=new Guid(flc.GetFieldValue("CommodityId"));
                    od.CommodityName = flc.GetFieldValue("CommodityName");

                    od.Amount=flc.GetFieldInt32("Amount");

                    od.AttributeDetailsName = flc.GetFieldValue("AttributeDetailsName");
                    od.CommodityAccessoriesName=flc.GetFieldValue("CommodityAccessoriesName");
                    
                    od.CommodityPriceId=new Guid(flc.GetFieldValue("CommodityPriceId"));
                    od.UnitPrice=flc.GetFieldValue("UnitPrice").TDecimal();
                    od.TotalPrice=flc.GetFieldValue("TotalPrice").TDecimal();
                    od.Discount=flc.GetFieldValue("Discount").TDecimal();
                    od.DiscountPrice=flc.GetFieldValue("DiscountPrice").TDecimal();
                    od.ActualTotalPrice=flc.GetFieldValue("ActualTotalPrice").TDecimal();
                    od.Rec_CreateBy = order.Rec_CreateBy;
                    od.Rec_ModifyBy = order.Rec_ModifyBy;
                    od.Disabled=0;
                    string promo=flc.GetFieldValue("PromotionsId");
                    if(!String.IsNullOrEmpty(promo))
                    {
                      od.PromotionsId=new Guid(promo);
                    }

                    //开始添加附件列表
                    IList<EOrderDetailsAccessories> listODC = new List<EOrderDetailsAccessories>();
                    EOrderDetailsAccessories odc = null;
                    var fllODC = flc.GetFieldList("OrderDetailsAccessoriesList");
                    foreach (var fllc in fllODC.FieldLists)
                    {
                        odc = new EOrderDetailsAccessories();
                        odc.OrderDetailsId = od.OrderDetailsId;
                        odc.OrderDetailsAccessoriesId = Guid.NewGuid();
                        odc.CommodityAccessoriesId = new Guid(fllc.GetFieldValue("CommodityAccessoriesId"));
                        odc.Rec_CreateBy = order.Rec_CreateBy;
                        odc.Rec_ModifyBy = order.Rec_ModifyBy;
                        odc.Disabled = 0;
                        listODC.Add(odc);
                    }
                    od.OrderDetailsAccessoriesList = listODC;

                    //开始添加属性列表
                    IList<EOrderDetailsAttribute> listODA=new List<EOrderDetailsAttribute>();
                    EOrderDetailsAttribute oda=null;
                    var fllODA=flc.GetFieldList("OrderDetailsAttributeList");
                    foreach(var flla in fllODA.FieldLists)
                    {
                        oda=new EOrderDetailsAttribute();
                        oda.OrderDetailsId=od.OrderDetailsId;
                        oda.OrderDetailsAttributeId=Guid.NewGuid();
                        oda.AttributeDetailsId=new Guid(flla.GetFieldValue("AttributeDetailsId"));
                        oda.Rec_CreateBy=order.Rec_CreateBy;
                        oda.Rec_ModifyBy=order.Rec_ModifyBy;
                        oda.Disabled=0;
                        listODA.Add(oda);
                    }
                    od.OrderDetailsAttributeList=listODA;

                    listOD.Add(od);
                }
                order.OrderDetailsList = listOD;
                //if (string.IsNullOrEmpty(user.UserName) || string.IsNullOrEmpty(user.Password) || string.IsNullOrEmpty(user.Mobile))
                //{
                //    rspMsg.Head.RspCode = MessageCode.Paramter_Empty.Code;
                //    rspMsg.Head.RspMsg = MessageCode.Paramter_Empty.Msg;
                //    return rspMsg;
                //}

               if (BLL.InsertOrder(order))
               {
                  rspMsg.Head.RspCode = MessageCode.Success.Code;
                  rspMsg.Head.RspMsg = MessageCode.Success.Msg;

                  return rspMsg;
                }
                else
                {
                   rspMsg.Head.RspCode = MessageCode.Server_Error.Code;
                   rspMsg.Head.RspMsg = MessageCode.Server_Error.Msg;

                   return rspMsg;
                }    
            }
            catch (Exception e)
            {

                ErrorLogWriter.Write("UGetCommodityListByShopId-" + e);
                throw e;
            }
        #endregion
         } 
    }
}